<?php

namespace App\Providers;

use Illuminate\Support\Facades\Route;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;

class RouteServiceProvider extends ServiceProvider
{
    /**
     * This namespace is applied to your controller routes.
     *
     * In addition, it is set as the URL generator's root namespace.
     *
     * @var string
     */
    protected $namespace = 'App\Http\Controllers';

    protected $namespace_common = 'App\Http\Controllers\Common';

    protected $namespace_open_wechat = 'App\Http\Controllers\OpenWechat';

    protected $namespace_app_v1 = 'App\Http\Controllers\App\V1';

    protected $namespace_webview_v1 = 'App\Http\Controllers\WebView\V1';

    protected $namespace_admin_v1 = 'App\Http\Controllers\Admin\V1';

    protected $namespace_debug = 'App\Http\Controllers\Debug';

    /**
     * Define your route model bindings, pattern filters, etc.
     *
     * @return void
     */
    public function boot()
    {
        $this->initMacros();

        parent::boot();
    }

    /**
     * Define the routes for the application.
     *
     * @return void
     */
    public function map()
    {
        $this->mapApiRoutes();

        $this->mapApiV1Routes();

        $this->mapWebRoutes();

        $this->mapOpenWechatRoutes();

        $this->mapDebugRoutes();
    }

    private function mapDebugRoutes()
    {
        if (! config('pkx.route_debug')) return;

        Route::middleware(['web'])
            ->prefix('debug')
            ->namespace($this->namespace_debug)
            ->group(base_path('routes/debug/debug.php'));
    }

    /**
     * Define the "web" routes for the application.
     *
     * These routes all receive session state, CSRF protection, etc.
     *
     * @return void
     */
    protected function mapWebRoutes()
    {
        Route::middleware('web')
             ->namespace($this->namespace)
             ->group(base_path('routes/web.php'));
    }

    /**
     * Define the "api" routes for the application.
     *
     * These routes are typically stateless.
     *
     * @return void
     */
    protected function mapApiRoutes()
    {
        Route::prefix('api')
             ->middleware('api')
             ->namespace($this->namespace)
             ->group(base_path('routes/api.php'));
    }


    /**
     * Define the "open wechat" routes for the application.
     *
     * These routes are typically stateless.
     *
     * @return void
     */
    protected function mapOpenWechatRoutes()
    {
        Route::prefix('open-wechat')
            ->namespace($this->namespace_open_wechat)
            ->group(base_path('routes/open-wechat/server.php'));

        Route::prefix('open-wechat')
            ->namespace($this->namespace_open_wechat)
            ->group(base_path('routes/open-wechat/authorizer.php'));

        Route::middleware(['web', 'Cors'])
            ->prefix('open-wechat')
            ->namespace($this->namespace_open_wechat)
            ->group(base_path('routes/open-wechat/oauth.php'));
    }

    /**
     * 绑定路由宏
     */
    public function initMacros()
    {
        /**
         * 跨域预检路由宏
         */
        Route::macro('corsAny', function ($uri = '/') {
            Route::options($uri . '{any}', '\\App\\Http\\Controllers\\CorsController@preFlightCheck')->where('any', '.*');
        });
    }

    private function mapApiV1Routes()
    {
        Route::prefix('api/common')
            ->middleware('api')
            ->namespace($this->namespace_common)
            ->group(base_path('routes/api/common.php'));

        Route::prefix('api/v1/app')
            ->middleware('api')
            ->namespace($this->namespace_app_v1)
            ->group(base_path('routes/api/v1/app.php'));

        Route::prefix('api/v1/webview')
            ->middleware('api')
            ->namespace($this->namespace_webview_v1)
            ->group(base_path('routes/api/v1/webview.php'));

        Route::prefix('api/v1/admin')
            ->middleware('api')
            ->namespace($this->namespace_admin_v1)
            ->group(base_path('routes/api/v1/admin.php'));
    }
}
